# -*- UTF-8 -*-
# 获得question_id 与 tag_id 关系对 .csv文件
import re

import pandas as pd
import numpy as np
import csv

# 统计最终输出的关系数目 50060824
def count(questions_csv_path):
    # 创建question_df 和 tag_df
    questions_df = pd.read_csv(questions_csv_path, usecols=['Id', 'Tags'])
    sum = 0
    # 遍历question_df（记录下来question_id）解析Tags属性，去tag_df中找到对应tag的tag_id
    pattern = re.compile(r"\<(.*?)\>", re.I | re.X)
    for row in questions_df.iterrows():
        tags_list = pattern.findall(row[1]['Tags'])
        sum = sum + len(tags_list)
    print(sum)

def get_quetion_tag_pair(questions_csv_path, tags_csv_path):
    # 创建question_df 和 tag_df
    questions_df = pd.read_csv(questions_csv_path, usecols=['Id', 'Tags'])

    tag_dic = {}
    with open(tags_csv_path, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            tag_dic[row['TagName']] = row['Id']

    print('---Open Succeed.')

    # 遍历question_df（记录下来question_id）解析Tags属性，去tag_dic中找到对应tag的tag_id
    pattern = re.compile(r"\<(.*?)\>", re.I | re.X)
    pair_index = 0
    pair_num = 50060824  # 实际获得的 pair数目 50060824
    pair_arr = np.empty(pair_num, dtype=map)

    for row in questions_df.iterrows():
        question_id = row[1]['Id']
        tags_list = pattern.findall(row[1]['Tags'])
        for tag in tags_list:
            pair_arr[pair_index] = {'QuestionId': question_id, 'TagId': tag_dic[tag]}
            pair_index = pair_index + 1
            if pair_index % 1000000 == 0:
                print("---Got " + str(pair_index))

    print('---All : ' + str(pair_index) + ' is succeed. Start write to csv...')

    pair_df = pd.DataFrame(pair_arr.tolist(), columns=['QuestionId', 'TagId'])

    # 将pair_df 写入csv文件
    pair_df.to_csv('Question_Tag.csv', index=False)

def write_csv():
    df = pd.DataFrame({'Id': [111, 222], 'Tags' : ['<c++><c#>', '<java><c++>']})

    df.to_csv("test.csv", index_label=False)

def test_tag(tags_csv_path):
    tag_dic = {}
    with open(tags_csv_path, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            tag_dic[row['TagName']] = row['Id']
    return tag_dic

if __name__ == '__main__':
    get_quetion_tag_pair("Questions.csv", "Tags.csv")
    # get_quetion_tag_pair("Questions.csv", "./csv_new/Tags.csv")

    # count("Questions.csv")
    # write_csv()
    # test_tag('Tags.csv')